<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upsapi extends CI_Controller {
	private $baseconfig;
	private $secretkey;
	public function __construct(){
		parent::__construct();
		$this->load->library('ComUploads');
		$this->load->library('ComImages');
		$this->up=new ComUploads();
		$this->img=new ComImages();
		$this->baseconfig=sys_config('base');
		$this->secretkey=$this->baseconfig['api_upload_token'];
		$this->updomian=PMSROOT;
		$this->checktoken();
	}
	//apitoken验证
	private function checktoken(){
		if(!isset($_GET['apitoken']) && !isset($_POST['apitoken']) ){
			ajax_return(0,'param is error');
		}
		if(!isset($_GET['time']) && !isset($_POST['time']) ){
			ajax_return(0,'param is error');
		}
		$apitoken=isset($_GET['apitoken'])?$_GET['apitoken']:$_POST['apitoken'];
		$time=isset($_GET['time'])?$_GET['time']:$_POST['time'];  //获取传值时间戳
		
		$datatime = microtime(time());   //获取当前的时间，格式是 1545881551.1733 微秒
		$datatime=round($datatime*1000,0); //转成毫秒
		$timediff = ($datatime-$time);  //当前时间减去传值时间的差	
		$mintime = $timediff/1000/60;  //时间相差的分钟数
		if(abs($mintime) > 5){
			ajax_return(0,"time is out");
		}
		$md5api_token = strtolower(md5($time.$this->secretkey));
		if($md5api_token!=$apitoken){
            ajax_return(0,"token is invalid");
		}
	}
	public function userface(){
		if(!isset($_FILES)){
			ajax_return(0,'无法获取上传素材，可能是文件超过服务器上传限制');
		}
		if(empty($_FILES)){
			ajax_return(0,'未获取到上传信息');
		}
		//获取配置信息
		$extsstr=$this->baseconfig['exts'];
		$exts=explode(',',$extsstr);
		$setconfig[]=array('exts',$exts);
		$setconfig[]=array('savePath','face');
		$setconfig[]=array('maxSize',$this->baseconfig['maxsize']);
		$this->up->setconfig($setconfig);
		$files=array_values($_FILES)[0];
		$info=$this->up->upload_one($files);
		//返回回调函数
		if(!$info){
			$geterrinfo=$this->up->getError();
			ajax_return(0,$geterrinfo[0]);
		}else{
			$arr=array();
			$arr['img']=trim($info[0]['filename'],'.');
			$arr['imgshow']=$this->updomian.trim($info[0]['filename'],'.');
			$arr['name']=$info[0]['name'];
			ajax_return(1,'上传成功',$arr);
		}
	}
	//素材上传
	public function attachment(){
		if(!isset($_FILES)){
			ajax_return(0,'无法获取上传素材，可能是文件超过服务器上传限制');
		}
		if(empty($_FILES)){
			ajax_return(0,'未获取到上传信息');
		}
		$files=$_FILES;
		//获取配置信息
		$imgstr=$this->baseconfig['attachment'];
		$imgarr=explode(',',$imgstr);
		$setconfig[]=array('exts',$imgarr);
		$setconfig[]=array('savePath','attachment');
		$setconfig[]=array('maxSize',$this->baseconfig['maxsize']);
		$this->up->setconfig($setconfig);
		$info=$this->up->uploads();
		//获取上传图片错误信息
		$returnerrinfo=array();
		$returnerrinfo=$this->up->getError();
		$newinfo=array();
		if(!empty($info)){
			foreach($info as $k=>$v){
				$fileimg=$v['filename'];
				$newinfo[$k]['img']=trim($fileimg,'.');
				$newinfo[$k]['imgshow']=$this->updomian.trim($fileimg,'.');
				$newinfo[$k]['name']=$v['name'];
			}
		}
		$errinfo='';
		if(!empty($returnerrinfo)){
			$errinfo=implode('<br />',$returnerrinfo);
		}else{
			$errinfo='上传成功';
		}
		if(empty($newinfo)){
			ajax_return(0,$errinfo);
		}
		$returninfo=array();

		foreach ($files as $k => $v) {
			if(isset($newinfo[$k])){
				$returninfo[$k]=$newinfo[$k];
			}else{
				$returninfo[$k]=null;
			}
		}
		ajax_return(1,$errinfo,$returninfo);
		
	}
}